package com.dzl.cardsecret;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.card.pojo.vo.WelfareCardExchangeSecretVo;
import com.dzl.card.pojo.vo.WelfareUserCardSecretVo;
import com.dzl.card.service.WelfareCardSecretService;
import com.dzl.card.service.WelfareCardService;
import com.dzl.constant.WelfareExchangeEnum;
import com.dzl.userwx.pojo.WelfareUserWx;
import com.dzl.userwx.service.WelfareUserWxService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.ImageUtils;
import com.dzl.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 卡密信息表(WelfareCardSecret)表控制层
 * @author ChenLong
 * @since 2021-05-14 20:19:50
 */
@Slf4j
@RestController
@RequestMapping("welfareCardSecret")
public class WelfareCardSecretController extends BaseController {

    @Autowired
    private WelfareCardSecretService welfareCardSecretService;
    @Autowired
    private WelfareUserWxService welfareUserWxService;
    @Autowired
    private WelfareCardService welfareCardService;

    /**
     * 增加用户绑定卡和手机号
     * @param jsonParams 用户id、卡密、电话、验证码
     * @return com.dzl.util.JsonResult
     * @author ChenLong
     * @since 2021-05-17 09:23:09
     **/
    @PostMapping("addUserCardAndPhoneBind")
    public JsonResult<String> addUserCardAndPhoneBind(@RequestBody JSONObject jsonParams) {
        log.info("addUserCardAndPhoneBind in jsonParams: " + jsonParams);
        Integer userId = getWxUserInfo().getId();
        String cardSecret = jsonParams.getString("cardSecret");
        String phone = jsonParams.getString("phone");
        // 电话号码不能为空
        if (EmptyUtils.isEmpty(phone)) {
            return JsonResult.paramErrWithMsgAndCode(1026, "请输入手机号码");
        }
        // 卡密不能为空
        if (EmptyUtils.isEmpty(cardSecret)) {
            return JsonResult.paramErrWithMsgAndCode(1025, "");
        }
        // 校验该手机号是否已被绑定
        WelfareUserWx welfareUserWxSelect = new WelfareUserWx();
        welfareUserWxSelect.setPhone(phone);
        welfareUserWxSelect.setEnterpriseId(getWxUserInfo().getEnterpriseId());
        Integer phoneCount = welfareUserWxService.selectCount(welfareUserWxSelect);
        if (phoneCount > 0) {
            return JsonResult.paramErrWithMsgAndCode(1026, "该手机号已被绑定");
        }
        // 添加该用户绑定手机号
        WelfareUserWx welfareUserWx = new WelfareUserWx();
        welfareUserWx.setId(userId);
        welfareUserWx.setPhone(phone);
        welfareUserWx.setUpdateTime(new Date());
        welfareUserWx.setEnterpriseId(getWxUserInfo().getEnterpriseId());
        Map<String, Object> resMap = welfareCardSecretService.addUserCardAndPhoneBind(welfareUserWx, cardSecret);
        if (resMap.get("code").equals(1)) {
            return JsonResult.success(resMap.get("data")+"");
        } else {
            return JsonResult.failWithMsg(resMap.get("msg").toString());
        }
    }

    /**
     * 增加用户绑定卡
     * @param cardSecret 卡密
     * @return com.dzl.util.JsonResult
     * @author ChenLong
     * @since 2021-05-17 10:20:47
     **/
    @PostMapping("addUserCardBind")
    public JsonResult<Integer> addUserCardBind(String cardSecret) {
        log.info("addUserCardBind in cardSecret: " + cardSecret);
        Integer userId = getWxUserInfo().getId();
        // 卡密不能为空
        if (EmptyUtils.isEmpty(cardSecret)) {
            return JsonResult.paramErrWithMsgAndCode(1025, "请输入卡密或兑换券信息");
        }
        Map resMap = welfareCardSecretService.addUserCardBind(cardSecret, userId, getWxUserInfo().getEnterpriseId());
        if (resMap.get("code").equals(1)) {
            return JsonResult.success();
        } else {
            return JsonResult.failWithMsg(resMap.get("msg").toString());
        }
    }

    /**
     * 通过密钥获取卡或兑换券信息
     * @param secret 密钥
     * @param type 1卡密 2券密
     * @return com.dzl.util.JsonResult
     * @author ChenLong
     * @date 2021-07-10 11:09:42
     **/
    @GetMapping("getCardExchangeInfoBySecret")
    public JsonResult<WelfareCardExchangeSecretVo> getCardExchangeInfoBySecret(String secret, Integer type) {
        log.info("getCardExchangeInfoBySecret in secret: " + secret);
        String typeName = "福力卡";
        if(EmptyUtils.isNotEmpty(type) && type == 2){
            typeName = "兑换券";
        }
        // 卡密不能为空
        if (EmptyUtils.isEmpty(secret)) {
            return JsonResult.paramErrWithMsg("请输入"+typeName+"信息");
        }
        // 获取卡密和兑换券集合
        List<WelfareCardExchangeSecretVo> list = welfareCardSecretService.getCardExchangeInfoBySecret(secret);
        if (EmptyUtils.isEmpty(list) || list.size() < 1) {
            return JsonResult.paramErrWithMsgAndCode(1025,"请输入正确的"+typeName+"信息");
        }
        if (list.size() > 1) {
            return JsonResult.failWithMsg(typeName+"重复");
        }
        if(list.get(0).getSecretType()!=type){
            if(type==1){
                return JsonResult.paramErrWithMsgAndCode(1025,"该密码为兑换券密码，请选择我有兑换券绑定");
            }else{
                return JsonResult.paramErrWithMsgAndCode(1025,"该密码为福力卡密码，请选择我有福力卡绑定");
            }
        }
        WelfareCardExchangeSecretVo welfareCardExchangeSecretVo = list.get(0);
        if(!welfareCardExchangeSecretVo.getEnterpriseId().equals(getWxUserInfo().getEnterpriseId())){
            return JsonResult.failWithMsg("无法在小程序绑定该卡");
        }
        if (EmptyUtils.isNotEmpty(welfareCardExchangeSecretVo.getStatus()) && welfareCardExchangeSecretVo.getIsActivate().equals(WelfareExchangeEnum.EXCHANGE_SECRET_ACTIVE_STATUS_BOUND.getCode())) {
            return new JsonResult<>(welfareCardExchangeSecretVo, 1025, "该"+typeName+"已被绑定");
        }
        if (EmptyUtils.isNotEmpty(welfareCardExchangeSecretVo.getStatus()) && welfareCardExchangeSecretVo.getIsActivate().equals(WelfareExchangeEnum.EXCHANGE_SECRET_ACTIVE_STATUS_USED.getCode())) {
            return new JsonResult<>(welfareCardExchangeSecretVo, 1025, "该"+typeName+"已被使用");
        }
        if (EmptyUtils.isNotEmpty(welfareCardExchangeSecretVo.getExpiredDate()) && welfareCardExchangeSecretVo.getExpiredDate().compareTo(new Date())==-1) {
            return new JsonResult<>(welfareCardExchangeSecretVo, 1025, "该"+typeName+"已过期");
        }
        if (EmptyUtils.isNotEmpty(welfareCardExchangeSecretVo.getIsActivate()) && welfareCardExchangeSecretVo.getIsActivate().equals(2)) {
            return new JsonResult<>(welfareCardExchangeSecretVo, 1025, "该"+typeName+"已被冻结，请联系客服");
        }
        if (EmptyUtils.isNotEmpty(welfareCardExchangeSecretVo.getIsActivate()) && welfareCardExchangeSecretVo.getIsActivate().equals(0)) {
            return new JsonResult<>(welfareCardExchangeSecretVo, 1025, "该"+typeName+"未激活");
        }
        return JsonResult.success(welfareCardExchangeSecretVo);
    }

    /**
     * 获取用户绑定的福利卡
     * @return com.dzl.util.JsonResult
     * @author ChenLong
     * @since 2021-05-19 16:12:00
     **/
    @GetMapping("getUserCardBindList")
    public JsonResult<List<WelfareUserCardSecretVo>> getUserCardBindList() {
        log.info("getUserCardBindList in");
        Integer userId = getWxUserInfo().getId();
        List<WelfareUserCardSecretVo> welfareCardSecretList = welfareCardSecretService.getUserCardBindList(userId);
        if (EmptyUtils.isNotEmpty(welfareCardSecretList)) {
            welfareCardSecretList.forEach(welfareUserCardSecretVo -> {
                if (EmptyUtils.isNotEmpty(welfareUserCardSecretVo.getImg())) {
                    welfareUserCardSecretVo.setImg(ImageUtils.setImageSrc(welfareUserCardSecretVo.getImg()));
                }
                /*if(welfareUserCardSecretVo.getIsActivate()== WelfareCardConstant.BOUND){
                    welfareUserCardSecretVo.setIsActivate(WelfareCardConstant.ACTIVATED);
                }*/
            });
        }
        return JsonResult.success(welfareCardSecretList);
    }

    /**
     * 获取用户绑定的福利卡详情
     * @author ChenLong
     * @date 2021-09-29 14:44:45
     * @param cardSecretId 卡秘ID
     * @return com.dzl.util.JsonResult<com.dzl.card.pojo.vo.WelfareUserCardSecretVo>
     **/
    @GetMapping("getUserCardBindInfo")
    public JsonResult<WelfareUserCardSecretVo> getUserCardBindInfo(Integer cardSecretId) {
        log.info("getUserCardBindInfo in");
        WelfareUserCardSecretVo welfareUserCardSecretVo = welfareCardSecretService.getUserCardBindInfo(cardSecretId);
        if (EmptyUtils.isNotEmpty(welfareUserCardSecretVo)) {
            welfareUserCardSecretVo.setImg(ImageUtils.setImageSrc(welfareUserCardSecretVo.getImg()));
        }
        return JsonResult.success(welfareUserCardSecretVo);
    }
}
